import 'package:get/get.dart';
// import '../../../models/focus__model.dart';
import '../../../models/category_model.dart';
// import '../../../models/plist_model.dart';
import '../../../services/httpsClient.dart';

class CategoryController extends GetxController {
  //TODO: Implement CategoryController

  HttpsClient httpsClient = HttpsClient();

  RxList<CategoryModelItem> leftCategroyList = <CategoryModelItem>[].obs;
  RxList<CategoryModelItem> rightCategroyList = <CategoryModelItem>[].obs;

  RxInt selectIndex = 0.obs;
  @override
  void onInit() {
    super.onInit();
    // 请求左侧列表数据
    getLeftCategoryData();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void changeIndex(int index, String pid) {
    selectIndex.value = index;
    getRightCategoryData(pid);
    update();
  }

  // 获取左侧分类列表
  void getLeftCategoryData() async {
    var res = await httpsClient.get('api/pcate');

    if (res != null) {
      var category = CategoryModel.fromJson(res.data);
      leftCategroyList.value = category.result;
      getRightCategoryData(leftCategroyList[0].id);
      update();
    }
  }

  void getRightCategoryData(String pid) async {
    var res = await httpsClient.get('api/pcate?pid=$pid');

    if (res != null) {
      var category = CategoryModel.fromJson(res.data);
      rightCategroyList.value = category.result;
      update();
    }
  }
}
